package com.jin.flashsaled.controller;


import com.jin.flashsaled.entity.User;
import com.jin.flashsaled.result.Good;
import com.jin.flashsaled.service.IGoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 靳鹏宇
 * @since 2025-09-18
 */
@Controller
@RequestMapping("/goods")
@Slf4j
public class GoodsController {

    @Autowired
    IGoodsService goodsService;

    @RequestMapping("/tolist")
    public String tolist(Model model, User user)
    {
        //如果cookie失效，返回登录界面
        if(user==null)
        {
            return "login";
        }
        model.addAttribute("user",user);
        model.addAttribute("goodsList",goodsService.getGoodsAllWithDetail());
        return "goodList";

    }

    @PostMapping("/todetail")
    public String toDetail(Model model, User user,@RequestParam("goodsId") Long goodsId)
    {
        //如果cookie失效，返回登录界面
        if(user==null)
        {
            return "login";
        }

        model.addAttribute("user",user);
        model.addAttribute("goods",goodsService.getGoodsWithDetailById(goodsId));
        return "goodDetail";

    }

}
